package com.sia.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sia.springcloud.model.UserInfo;
import com.sia.springcloud.result.GridDataResult;
import com.sia.springcloud.result.ResultUtil;
import com.sia.springcloud.service.UserInfoService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Author: Sirius
 * @Date: 2022/3/7 13:13
 * @Description:
 * 管理员的用户管理方法：添加用户、分页查询用户、批量删除用户、修改用户信息
 *
 * 只有管理员用户才能访问
 *
 **/
@Slf4j
@RestController
@RequestMapping("//userController")
public class UserController {

    @Resource
    private UserInfoService userInfoService;


    /**
     * 添加用户
     *
     * @param request
     * @return
     */
    @RequestMapping("/insertUser")
    public String insertUser(HttpServletRequest request) {
        try {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String permission = request.getParameter("permission");
            String email = request.getParameter("email");
            String company = request.getParameter("company");
            String telephone = request.getParameter("telephone");
            UserInfo userInfo = new UserInfo();

            if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
                return ResultUtil.createSimpleFailureJson("用户名和密码不能为空");
            }
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            if (!StringUtils.isEmpty(permission) && "0".equals(permission)) {
                userInfo.setRole("admin");
                userInfo.setPermission(0);
            } else {
                userInfo.setRole("commonUser");
                userInfo.setPermission(1);
            }
            if (!StringUtils.isEmpty(email)) {
                userInfo.setEmail(email);
            }
            if (!StringUtils.isEmpty(company)) {
                userInfo.setCompany(company);
            }
            if (!StringUtils.isEmpty(telephone)) {
                userInfo.setTelephone(telephone);
            }
            return userInfoService.insertUser(userInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.createSimpleFailureJson("用户新增失败");
    }

    /**
     * 分页查询用户信息
     *
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUser")
    public String queryUser(HttpServletRequest request) {
        String start = request.getParameter("start");
        String limit = request.getParameter("limit");
        if(StringUtils.isEmpty(start)||StringUtils.isEmpty(limit)){
            return ResultUtil.createSimpleFailureJson("查询条件不能为空");
        }

        return userInfoService.queryUser(start, limit);
    }

    /**
     * 批量删除用户
     *
     * @param request
     * @return
     */
    @RequestMapping("/deleteById")
    public String deleteById(HttpServletRequest request) {
        String ids = request.getParameter("id");
        if(StringUtils.isEmpty(ids)){
            return ResultUtil.createSimpleFailureJson("不能为空");

        }
        String[] idList = request.getParameter("id").split(",");

        return userInfoService.deleteById(idList);
    }

    /**
     * 修改用户信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(HttpServletRequest request) {
        try {

            String id = request.getParameter("id");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String permission = request.getParameter("permission");
            String email = request.getParameter("email");
            String company = request.getParameter("company");
            String telephone = request.getParameter("telephone");
            UserInfo userInfo = new UserInfo();

            if (StringUtils.isEmpty(id)||StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
                return ResultUtil.createSimpleFailureJson("用户名和密码不能为空");
            }
            userInfo.setId(Integer.parseInt(id));
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            if (!StringUtils.isEmpty(permission) && "0".equals(permission)) {
                userInfo.setRole("admin");
                userInfo.setPermission(0);
            } else {
                userInfo.setRole("commonUser");
                userInfo.setPermission(1);
            }
            if (StringUtils.isNotEmpty(email)) {
                userInfo.setEmail(email);
            }
            if (!StringUtils.isNotEmpty(company)) {
                userInfo.setCompany(company);
            }
            if (!StringUtils.isNotEmpty(telephone)) {
                userInfo.setTelephone(telephone);
            }
            return userInfoService.updateUser(userInfo);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.createSimpleFailureJson("用户修改失败");

    }

}
